package com.ykh.config;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.ykh.pojo.vo.ErrorCode;
import com.ykh.pojo.vo.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理
 */
@ControllerAdvice
public class AllExceptionHandler {

    //进行异常处理, 处理Exception.class的异常
    @ExceptionHandler(IllegalArgumentException.class)
    //让返回的数据时josn数据
    @ResponseBody
    public Result doException(IllegalArgumentException ex) {
        ex.printStackTrace();
        return Result.fail(-999,"数据库操作异常");
    }

    //进行异常处理, 处理Exception.class的异常
    @ExceptionHandler(TokenExpiredException.class)
    //让返回的数据时josn数据
    @ResponseBody
    public Result doException(TokenExpiredException ex) {
        ex.printStackTrace();
        return Result.fail(ErrorCode.TOKEN_EXPIRE.getCode(),ErrorCode.TOKEN_EXPIRE.getMsg());
    }

    //进行异常处理, 处理Exception.class的异常
    @ExceptionHandler(Exception.class)
    //让返回的数据时josn数据
    @ResponseBody
    public Result doException(Exception ex) {
        ex.printStackTrace();
        return Result.fail(-9989,"系统异常");
    }


}